package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.woniuhis.entity.Medicine;
import com.woniuxy.woniuhis.exception.MedicineServiceException;
import com.woniuxy.woniuhis.service.MedicineService;
import com.woniuxy.woniuhis.util.ResponseInfo;

@Controller
public class MedicineController {

	@Autowired
	MedicineService medicineService;

	/**
	 * @param req
	 * @return 删除药物的controller方法
	 */
	@RequestMapping("deleteMedicine")
	@ResponseBody
	public ModelAndView deleteMedicineController(String id) {
		try {

			Medicine medicine = new Medicine();
			if (id != null && id != "") {
				medicine.setMediId(id);
			} else {
				throw new NullPointerException();
			}

			medicineService.delMedicineByMediID(medicine);

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("删除药品成功");
			responseInfo.setState(200);

			ModelAndView mv = new ModelAndView();
			mv.setViewName("views/pharmacy/medimanage.html");
			mv.addObject("info", responseInfo);

			return mv;
		} catch (MedicineServiceException e) {
			System.out.println("deleteMedicineController错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("deleteMedicineController服务方法错误");
			responseInfo.setState(500);
			ModelAndView mv = new ModelAndView();
			mv.setViewName("views/pharmacy/medimodify.html");
			mv.addObject("info", responseInfo);

			return mv;
		} catch (NullPointerException e) {
			System.out.println("不完整错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("请输入全部的数据");
			responseInfo.setState(500);
			ModelAndView mv = new ModelAndView();
			mv.setViewName("views/pharmacy/medimodify.html");
			mv.addObject("info", responseInfo);

			return mv;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("其他错误");
			responseInfo.setState(500);
			ModelAndView mv = new ModelAndView();
			mv.setViewName("views/pharmacy/medimodify.htm");
			mv.addObject("info", responseInfo);

			return mv;
		}

	}

	/**
	 * @param req
	 * @return 修改药物的controller方法
	 */
	@RequestMapping("modifyMedicine")
	@ResponseBody
	public ResponseInfo modifyMediController(HttpServletRequest req) {

		System.out.println("modifyMediController");
		String id = req.getParameter("id");
		System.out.println(id);
		String name = req.getParameter("name");
		System.out.println(name);
		String standard = req.getParameter("standard");
		System.out.println(standard);
		String specification = req.getParameter("specification");
		System.out.println(specification);
		String manufacture = req.getParameter("manufacture");
		System.out.println(manufacture);
		String price = req.getParameter("price");
		System.out.println(price);

		try {

			Medicine medicine = new Medicine();
			if (id != null && id != "") {
				medicine.setMediId(id);
			} else {
				throw new NullPointerException();
			}
			if (name != null && name != "") {
				medicine.setMediName(name);
			} else {
				throw new NullPointerException();
			}
			if (standard != null && standard != "") {
				medicine.setMediStand(standard);
			} else {
				throw new NullPointerException();
			}
			if (specification != null && specification != "") {
				medicine.setMediSpecification(specification);
			} else {
				throw new NullPointerException();
			}
			if (manufacture != null && manufacture != "") {
				medicine.setMediManu(manufacture);
			} else {
				throw new NullPointerException();
			}
			if (price != null && price != "") {
				medicine.setMediPrice(Double.parseDouble(price));
			} else {
				throw new NullPointerException();
			}

			medicineService.modifyMedicine(medicine);

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("修改药品成功");
			responseInfo.setState(200);

			return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("modifyMediController错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("modifyMediController服务方法错误");
			responseInfo.setState(500);
			return responseInfo;
		} catch (NullPointerException e) {
			System.out.println("不完整错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("请输入全部的数据");
			responseInfo.setState(500);
			return responseInfo;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("其他错误");
			responseInfo.setState(500);
			return responseInfo;
		}

	}

	/**
	 * @param req
	 * @return 跳转到修改药物页面的controller方法
	 */
	@RequestMapping("toModifyMedicine")
	@ResponseBody
	public ModelAndView toModifyMedicineController(HttpServletRequest req, HttpServletResponse resp, Model model) {

		System.out.println("toModifyMedicineController");
		String id = req.getParameter("id");
		System.out.println(id);
		String name = req.getParameter("name");
		System.out.println(name);
		String standard = req.getParameter("standard");
		System.out.println(standard);
		String specification = req.getParameter("specification");
		System.out.println(specification);
		String manufacture = req.getParameter("manufacture");
		System.out.println(manufacture);
		String price = req.getParameter("price");
		System.out.println(price);

		ModelAndView mv = new ModelAndView();

		try {

			Medicine medicine = new Medicine();
			if (id != null && id.length() > 0) {
				medicine.setMediId(id);
			} else {
				id = "未设定";
				medicine.setMediId(id);
			}

			if (name != null && name.length() > 0) {
				medicine.setMediName(name);
			} else {
				name = "未设定";
				medicine.setMediName(name);
			}

			if (standard != null && standard.length() > 0) {
				medicine.setMediStand(standard);
			} else {
				standard = "未设定";
				medicine.setMediStand(standard);
			}

			if (specification != null && specification.length() > 0) {
				medicine.setMediSpecification(specification);
			} else {
				specification = "未设定";
				medicine.setMediSpecification(specification);
			}

			if (manufacture != null && manufacture.length() > 0) {
				medicine.setMediManu(manufacture);
			} else {
				manufacture = "未设定";
				medicine.setMediManu(manufacture);
			}

			if (price == null && price.length() > 0) {
				System.out.println("xxx");
				medicine.setMediPrice(Double.parseDouble(price));
			} else {
				System.out.println("2");
				price = "0";
				medicine.setMediPrice(Double.parseDouble(price));
			}

			System.out.println(id);
			System.out.println(name);
			System.out.println(standard);
			System.out.println(specification);
			System.out.println(manufacture);
			System.out.println(price);

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setData(medicine);
			responseInfo.setMsg("跳转页面成功");
			responseInfo.setState(200);

			mv.setViewName("views/pharmacy/medimodify.html");
			mv.addObject("info", responseInfo);

			return mv;
		} catch (MedicineServiceException e) {
			System.out.println("tomodifyMedicine错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("tomodifyMedicine服务方法错误");
			responseInfo.setState(500);

			mv.setViewName("views/pharmacy/medimodify.html");
			mv.addObject("info", responseInfo);

			return mv;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("其他错误");
			responseInfo.setState(500);

			mv.setViewName("views/pharmacy/medimodify.html");
			mv.addObject("info", responseInfo);

			return mv;
		}

	}

	/**
	 * @param req
	 * @return 查询药物的controller方法
	 */
	@RequestMapping("findMedicine")
	@ResponseBody
	public ResponseInfo findAllMedicineController(HttpServletRequest req) {

		String mediname = req.getParameter("mediname");
		// System.out.println(mediname);
		Medicine medicine = new Medicine();
		medicine.setMediName(mediname);

		try {
			List<Medicine> medis = medicineService.findMedicineByName(medicine);

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setData(medis);
			responseInfo.setMsg("查询药物");
			responseInfo.setState(200);

			return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("findAllMedicineController错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("findMedicineByName服务方法错误");
			responseInfo.setState(500);
			return responseInfo;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("其他错误");
			responseInfo.setState(500);
			return responseInfo;
		}
	}

	/**
	 * @param req
	 * @return 添加药物的controller方法
	 */
	@RequestMapping("addMedicine")
	@ResponseBody
	public ResponseInfo addMedicineController(HttpServletRequest req) {

		String name = req.getParameter("name");
		System.out.println(name);
		String standard = req.getParameter("standard");
		String specification = req.getParameter("specification");
		String manufacture = req.getParameter("manufacture");
		String price = req.getParameter("price");

		try {

			Medicine medicine = new Medicine();
			if (name != null && name != "") {
				medicine.setMediName(name);
			} else {
				throw new NullPointerException();
			}
			if (standard != null && standard != "") {
				medicine.setMediStand(standard);
			} else {
				throw new NullPointerException();
			}
			if (specification != null && specification != "") {
				medicine.setMediSpecification(specification);
			} else {
				throw new NullPointerException();
			}
			if (manufacture != null && manufacture != "") {
				medicine.setMediManu(manufacture);
			} else {
				throw new NullPointerException();
			}
			if (price != null && price != "") {
				medicine.setMediPrice(Double.parseDouble(price));
			} else {
				throw new NullPointerException();
			}

			medicineService.addMedicine(medicine);

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("添加药品成功");
			responseInfo.setState(200);

			return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("addMedicine错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("addMedicine服务方法错误");
			responseInfo.setState(500);
			return responseInfo;
		} catch (NullPointerException e) {
			System.out.println("不完整错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("请输入全部的数据");
			responseInfo.setState(500);
			return responseInfo;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();

			ResponseInfo responseInfo = new ResponseInfo();
			responseInfo.setMsg("其他错误");
			responseInfo.setState(500);
			return responseInfo;
		}

	}
}
